<!--
 * @Description: ------------ fileDescription -----------
 * @Author: snows_l snows_l@163.com
 * @Date: 2024-08-09 16:19:38
 * @LastEditors: snows_l snows_l@163.com
 * @LastEditTime: 2025-11-06 09:38:21
 * @FilePath: \BLOG\src\components\common\PageTopCover.vue
-->

<!--
  * @Description: 页面top封面组件
  *props:
  * coverImg: 封面图片地址
  * moduleTitle: 模块标题
  * modulDesc: 模块描述
  * imgIcon: 图标图片地址 优先级 imgIcon > textIcon > icon
  * textIcon: 图标文字
  * icon: 图标类名
  * isMobile: 是否是移动端
  * articleInfo: 文章信息
-->
<template>
  <div class="top-cover-out-big-wrap" :class="{ 'm-top-cover-out-big-wrap': isMobile }">
    <div class="page-top-cover-wrap">
      <img id="cover-img" class="cover-img" :src="coverImg" />
      <div class="mask-wrap" v-if="state.imgLoading"></div>
      <div class="content-wrap">
        <div class="mudule">
          <img class="icon-img iconfont" v-if="imgIcon" :src="getImgIcon(imgIcon)" alt="" />
          <span v-else-if="textIcon" class="iconfont">{{ textIcon }}</span>
          <i v-else class="iconfont" :class="icon"></i>
          <span class="text">{{ moduleTitle }}</span>
        </div>
        <div class="title-wrap text" v-if="modulDesc">{{ modulDesc }}</div>
        <div class="article-info" v-if="path == '/article/detail'">
          <div class="sum">
            <div class="sum-item">
              <IconHot class="iconfont"></IconHot>
              <span>{{ readCount || 0 }}</span>
            </div>
            <div class="sum-item">
              <IconLike :size="18" class="iconfont" />
              <span>{{ shareCount || 0 }}</span>
            </div>
          </div>

          <div class="label-wrap">
            <div
              class="label-item pointer"
              v-for="(label, index) in labels"
              :key="index"
              @click="handleCategory(label)"
              :style="{ marginRight: index === labels.length - 1 ? '0' : '10px' }">
              <span class="icon-wrap">
                <IconLabel class="iconfont" :size="'14px'"></IconLabel>
              </span>
              <span class="lable" :style="{ 'background-color': colorMap[index] }">{{ label }}</span>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { useRoute, useRouter } from 'vue-router';
import { getRandomColor } from '@/utils/theme';
import { getImgIcon } from '@/utils/common';
import { onMounted, reactive } from 'vue';

interface Props {
  coverImg?: string;
  moduleTitle?: string;
  modulDesc?: string;
  icon?: string;
  imgIcon?: string;
  textIcon?: string;
  isMobile?: boolean;
  articleInfo?: any;
  readCount?: number;
  shareCount?: number;
  labels?: string[];
}

const props = withDefaults(defineProps<Props>(), {
  coverImg: '',
  moduleTitle: '',
  modulDesc: '',
  icon: '',
  imgIcon: '',
  textIcon: '',
  isMobile: false,
  articleInfo: () => ({}),
  readCount: 0,
  shareCount: 0,
  labels: () => []
});

const colorMap = {
  0: getRandomColor(),
  1: getRandomColor(),
  2: getRandomColor(),
  3: getRandomColor(),
  4: getRandomColor(),
  5: getRandomColor(),
  6: getRandomColor(),
  7: getRandomColor(),
  8: getRandomColor(),
  9: getRandomColor()
};
const router = useRouter();
const path = useRoute().path;

const state = reactive({
  imgLoading: false
});

// 监听图片加载状态
const watchImgLoading = () => {
  const img = document.getElementById('cover-img');
  (img as HTMLElement).addEventListener('load', function () {
    state.imgLoading = true;
  });
  (img as HTMLElement).addEventListener('error', function () {
    state.imgLoading = true;
  });
};

// 点击文章分类
const handleCategory = (label: string) => {
  router.push({
    path: '/article/category',
    query: {
      type: 4,
      category: label
    }
  });
};

onMounted(() => {
  watchImgLoading();
});
</script>

<style lang="scss" scoped>
.top-cover-out-big-wrap {
  // background: var(--under-background);
  width: 100%;
  height: 400px;
  .page-top-cover-wrap {
    width: 100%;
    height: 100%;
    position: relative;
    background-image: url('@/assets/images/bg/default-cover.png');
    &::before {
      content: '';
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background-color: #ffffff65;
    }
    .cover-img {
      width: 100%;
      height: 100%;
      object-fit: cover;
      position: absolute;
      top: 0;
      left: 0;
    }
    .mask-wrap {
      width: 100%;
      height: 100%;
      object-fit: cover;
      position: absolute;
      top: 0;
      left: 0;
      background-color: var(--bg-mask-color);
    }
    .content-wrap {
      position: absolute;
      left: 50%;
      transform: translateX(-50%);
      bottom: 0px;
      max-width: var(--content-max-width);
      width: 100%;
      padding: 20px 0;
      border-radius: var(--border-radius-1);
      display: flex;
      flex-direction: column;

      .mudule {
        height: 36px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        .iconfont {
          margin-right: 10px;
          color: #fff;
          font-size: 20px;
          text-shadow: var(--text-shadow);
          filter: drop-shadow(4px 4px 4px #000);
        }
        .icon-img {
          width: 36px;
          height: 36px;
          margin-right: 20px;
          text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
          filter: drop-shadow(4px 4px 4px #000);
        }
        .text {
          color: #fff;
          font-size: 20px;
          text-shadow: var(--text-shadow);
          filter: drop-shadow(4px 4px 4px #000);
        }
      }
      .title-wrap {
        margin-top: 10px;
        font-size: 16px;
        margin-left: 4px;
        color: var(--text-color);
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
        text-overflow: ellipsis;
        color: #fff;
        text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
        filter: drop-shadow(4px 4px 4px #000);
      }
    }
    .default-bg-color {
      // background-color: var(--bg-cover-color);
    }
    .article-info {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-top: 15px;
      .sum {
        display: flex;
        .sum-item {
          margin-right: 30px;
          display: flex;
          align-items: center;
          .iconfont {
            margin-right: 10px;
          }
          span {
            color: #fff;
          }
        }
      }
      .label-wrap {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        margin-top: 10px;
        .type {
          font-size: 12px;
          border-radius: 5px;
          margin-right: 15px;
          height: 20px;
          display: flex;
          align-items: center;
          overflow: hidden;
          margin-bottom: 2px;
          .icon-wrap {
            padding: 1px 6px 1px 8px;
          }
        }
        .label-item {
          margin-right: 10px;
          font-size: 12px;
          display: flex;
          align-items: center;
          border-radius: var(--border-radius-1);
          overflow: hidden;
          height: 20px;
          margin-bottom: 2px;
        }
        .icon-wrap,
        .lable {
          height: 100%;
          display: inline-block;
        }
        .icon-wrap {
          padding: 3px 6px 3px 8px;
          background-color: #b3b0b0;
        }
        .lable {
          color: #fff;
          padding: 4px 10px 4px 6px !important;
          font-size: 12px;
          text-wrap: nowrap;
        }
      }
    }
  }
}

.m-top-cover-out-big-wrap {
  height: 300px !important;
  .page-top-cover-wrap {
    .content-wrap {
      max-width: var(--content-max-width-m) !important;
    }
  }
}
</style>
